<script setup lang="ts">
  import { watch } from 'vue';
  import { $t } from '@/locales';
  import { useAppStore } from '@/store/modules/app';
  import { useEcharts } from '@/hooks/common/echarts';

  defineOptions({
    name: 'LineChart',
  });
  interface Props {
    data: any;
  }
  const props: any = defineProps<Props>();

  const appStore = useAppStore();

  const { domRef, updateOptions } = useEcharts(() => ({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985',
        },
      },
    },
    legend: {
      data: [
        // $t('page.home.downloadCount'),
        $t('page.home.registerCount'),
      ],
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: [] as string[],
    },
    yAxis: {
      type: 'value',
    },
    series: [
      {
        color: '#8e9dff',
        name: $t('page.home.registerCount'),
        type: 'line',
        smooth: true,
        stack: 'Total',
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0.25,
                color: '#8e9dff',
              },
              {
                offset: 1,
                color: '#fff',
              },
            ],
          },
        },
        emphasis: {
          focus: 'series',
        },
        data: [] as number[],
      },
      // {
      //   color: '#26deca',
      //   name: $t('page.home.registerCount'),
      //   type: 'line',
      //   smooth: true,
      //   stack: 'Total',
      //   areaStyle: {
      //     color: {
      //       type: 'linear',
      //       x: 0,
      //       y: 0,
      //       x2: 0,
      //       y2: 1,
      //       colorStops: [
      //         {
      //           offset: 0.25,
      //           color: '#26deca'
      //         },
      //         {
      //           offset: 1,
      //           color: '#fff'
      //         }
      //       ]
      //     }
      //   },
      //   emphasis: {
      //     focus: 'series'
      //   },
      //   data: []
      // }
    ],
  }));

  async function mockData() {
    await new Promise((resolve) => {
      setTimeout(resolve, 1000);
    });

    updateOptions((opts) => {
      const cols = [];
      const row = [];
      for (let i = 0; i < props.data?.length; i++) {
        cols.push(props.data[i].time);
        row.push(props.data[i].count);
      }
      opts.xAxis.data = cols;
      opts.series[0].data = row;
      // opts.series[1].data = [2208, 2016, 2916, 4512, 8281, 2008, 1963, 2367, 2956, 678];

      return opts;
    });
  }

  function updateLocale() {
    updateOptions((opts, factory) => {
      const originOpts = factory();

      opts.legend.data = originOpts.legend.data;
      opts.series[0].name = originOpts.series[0].name;
      // opts.series[1].name = originOpts.series[1].name;

      return opts;
    });
  }

  async function init() {
    mockData();
  }

  watch(
    () => props.data,
    () => {
      updateLocale();
      mockData();
    }
  );

  // init
  init();
</script>

<template>
  <NCard :bordered="false" class="card-wrapper">
    <div ref="domRef" class="h-360px overflow-hidden"></div>
  </NCard>
</template>

<style scoped></style>
